// axios;
// clipboardy;
const chalk = require("chalk");
const { default: axios } = require("axios");
var ncp = require("node-clipboardy");
const MD5 = require("md5");
const prog = require("commander");

const appid = "20220418001179097";
const key = "ocRK8gagkaKloFMiuWLt";

// 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
var from = "en";
var to = "zh";
const log = console.log;

async function translate() {
	const enStr = await ncp.read();
	const salt = new Date().getTime();
	const query = enStr;
	const sign = MD5(appid + query + salt + key);

	axios
		.get("https://fanyi-api.baidu.com/api/trans/vip/translate", {
			params: {
				q: query,
				appid: appid,
				salt: salt,
				from: from,
				to: to,
				sign: sign,
			},
		})
		.then((res) => {
			const reslt = res.data.trans_result[0];
			if (prog.program.opts().write) {
				if (!reslt.dst) ncp.writeSync("\n翻译歇逼了,在搞一下看看");
				else ncp.writeSync(reslt.dst);
			}

			log(
				chalk.red(reslt.src) + chalk.blue("  =>  ") + chalk.greenBright(reslt.dst)
			);
		})
		.catch((e) => {
			log(chalk.red.bold("\n翻译歇逼了,在搞一下看看?"));
			return e;
		});
}
module.exports = { translate };
